﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace tscsclassgen
{
    class GenCS
    {
        internal static string ToCSClass(IEnumerable<TypeNamePair> typeNameMapping, string name)
        {
            var found = typeNameMapping.FirstOrDefault(c => c.TypeScriptName == name);
            if (found == null) return name;
            return found.CSharpName;
        }
        internal static string Generate(IEnumerable<ClassDef> classes, IEnumerable<TypeNamePair> typeNameMapping)
        {
            var sb = new StringBuilder();
            sb.AppendLine("// generated by tscsclassgen");
            sb.AppendFormat("// {0}\r\n", DateTime.Now);
#if USE_SERIALIZATION
            sb.AppendLine("// please add reference to System.Runtime.Serialization.");
            sb.AppendLine("// and follow two lines.");
            sb.AppendLine("using System.Runtime.Serialization;");
            sb.AppendLine("using System.Runtime.Serialization.Json;");
#endif
            sb.AppendLine();
            foreach (var item in classes)
            {
#if USE_SERIALIZATION
                sb.AppendLine("[DataContract]");
#endif
                sb.AppendFormat("public class {0}", ToCSClass(typeNameMapping, item.ThisClassName));
                if (item.SuperClassName != "Object")
                {
                    sb.AppendFormat(" : {0}", ToCSClass(typeNameMapping, item.SuperClassName));
                }
                sb.AppendLine("{");
                foreach (var item2 in item.Members)
                {
                    sb.AppendFormat("    public {1} {0};\r\n", ToCSClass(typeNameMapping, item2.MemberName), ToCSClass(typeNameMapping, item2.TypeName));
                }
                sb.AppendLine("}");
            }
            return sb.ToString();
        }
    }
}
